/*author dm*/
var sex = "m";
var time = 5;
var timer;
$(document).ready(function()
{
	
	$("#male").click(function(){chSex(this,"male");sex = "m"});
	$("#female").click(function(){chSex(this,"female");sex = "f"});
	$("#submit").click(function(){submitRegData()});
});

function submitRegData()
{
	//check input 学号 to be 13number;
	var name = $("#name").val();
	
	//name = "alex";
	
	if($.isEmptyObject(name))
	{
		showErrorMsg("#nametip","#tip_namemsg","姓名不能为空");
		return;
	}
	var sid = $("#sid").val();
	
	//sid = "2010231010013";
	
	if($.isEmptyObject(sid))
	{
		showErrorMsg("#acctip","#tip_accmsg","学号不能为空");
		return;
	}
	if(!$.isNumeric(sid) || !(sid.length == 13))
	{
		showErrorMsg("#acctip","#tip_accmsg","学号只能为13位数字");
		return;
	}
	var pwd = $("#pwd").val();
	
	//pwd = "123456";
	
	var apwd = $("#assupwd").val();
	
	//apwd = "123456"
	
	if(pwd.length < 6 || pwd.length > 15)
	{
		showErrorMsg("#pwdtip","#tip_pwdmsg","密码长度必须是6到15个字符");
		return;
	}
	
	if(pwd != apwd)
	{
		showErrorMsg("#assupwdtip","#tip_assupwdmsg","两次输入密码不匹配");
		return;
	}
	var regdata = 'regdata={"name":"'+name+'","sid":"'+sid+'","pwd":"'+pwd+'","sex":"'+sex+'"}';
	//输入无误，进行注册
	$.post("../php/register.php",regdata,function(data)
	{
		showInfoMsg("#regresult",data);
		
	},"text");
}

function showErrorMsg(eledivid,elemsgid,msg)
{
	$(elemsgid).html(msg);
	changCssAttr(eledivid,"display","block");
	$(eledivid).fadeIn().delay(2000).fadeOut("slow",function()
	{
		changCssAttr(eledivid,"display","none");
	});
}

function showInfoMsg(eledivid,msg)
{
	var head = msg.charAt(0);
	if(head == 'e')
	{
		msg = msg.slice(1);
		$(eledivid).html("<span class='erromsg'>"+msg+"</span>");
		changCssAttr(eledivid,"display","block");
		$(eledivid).fadeIn().delay(2000).fadeOut("slow",function()
		{
			changCssAttr(eledivid,"display","none");
		});
	}
	else if(head == 's')
	{
		msg = msg.slice(1);
		time = 5;
		$(eledivid).html("<div class='succussmsg'>"+msg+" <span class='erromsg'>"+time+"</span>  秒后跳转到登录界面</div>");
		changCssAttr(eledivid,"display","block");
		time--;
		timer = setInterval(function()
		{
			redirect(eledivid,msg);
		},1000);
	}
	else
	{
		$(eledivid).html("<span class='erromsg'>"+msg+"</span>");
	}
}
function redirect(eledivid,msg)
{
	$(eledivid).html("<div class='succussmsg'>"+msg+" <span class='erromsg'>"+time+"</span>  秒后跳转到登录界面</div>");
	time--;
	if(time < 1)
	{
		$(eledivid).fadeOut("slow",function()
		{
			changCssAttr(eledivid,"display","none");
		});
		clearInterval(timer);
		//$("head").append("<meta http-equiv='refresh' content='0;url='../html/login.html'>");
		window.location = "../html/login.html";
	}
}

function chSex(ele,sex)
{
	if(sex == "male")
	{
		$(ele).removeClass("unchecked").addClass("checked");
		$("#female").removeClass("checked").addClass("unchecked");
	}
	else
	{
		$(ele).removeClass("unchecked").addClass("checked");;
		$("#male").removeClass("checked").addClass("unchecked");
	}
}
/*改变一个元素css的一个属性*/
function changCssAttr(element,cssName,cssValue,condition)
{
	if(condition || condition == null)
	{
		$(element).css(cssName,cssValue);
	}
}